package gov.pbc.gx.nn.pz.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

import gov.pbc.gx.nn.pz.entity.HcBwpz;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

/**
 * 
 * @author jack.zhao 本类主要操作数据库CURD，以及其他的操作！
 */
@Repository
public class BwpzDao extends BaseImageDao {
	public BwpzDao() {
		super();

	}

	private static final Integer PZ_CODE_ = 7;

	/*
	 * 判断数据库中是否有相同的值
	 */
	public boolean isSameValue(HcBwpz hcBwpz) {
		String type = hcBwpz.getType();
		String pztemplate = hcBwpz.getPzTemplate();
		String hql = " from HcBwpz where deptId=:deptId and type=:type and pzTemplate=:pztemplate and invalid=false";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		map.put("type", type);
		map.put("pztemplate", pztemplate);
		List<HcBwpz> hcBwpzdatas = this.query(hql, map);
		if (!hcBwpzdatas.isEmpty()) {
			return true;
		}
		return false;
	}

	/*
	 * 保存实体
	 */
	public void saveBwpz(HcBwpz hcBwpz) {
		if (!isSameValue(hcBwpz)) {
			System.out.println(hcBwpz.getSfMark());
			hcBwpz.setId(null);
			hcBwpz.setDeptId(this.getDeptId());
			this.save(hcBwpz);
		} else {
			// hcBwpz.setId(bwpzId);
			// hcBwpz.setDeptId(this.getDeptId());
			this.update(hcBwpz);
		}
	}

	/*
	 * 根据type，pztemplate得到实体
	 */
	public HcBwpz selectOneByTypeAndTemplate(String type, String pztemplate) {
		List<HcBwpz> hcBwpzs = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("pztemplate", pztemplate);
		if (type == null) {
			String hql = " from HcBwpz where deptId=:deptid and type is null and pzTemplate=:pztemplate and invalid=false";
			hcBwpzs = this.query(hql, map);
		} else {
			String hql = " from HcBwpz where deptId=:deptid and type=:type and pzTemplate=:pztemplate and invalid=false";
			map.put("type", type);
			hcBwpzs = this.query(hql, map);
		}
		if (!hcBwpzs.isEmpty()) {
			return hcBwpzs.get(0);
		}
		return null;
	}

	/*
	 * 根据type得到实体的集合；
	 */
	public Collection<HcBwpz> getCoByType(String type) {
		Collection<HcBwpz> hcBwpzs;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		if (type == null) {
			String hql = " from HcBwpz where deptId=:deptid and  invalid = false and type is null";
			hcBwpzs = this.query(hql, map);
		} else {
			String hql = " from HcBwpz where deptId=:deptid and invalid = false and type=:type";
			map.put("type", type);
			hcBwpzs = this.query(hql, map);
		}
		return hcBwpzs;
	}

	/*
	 * 没有参数，得到实体按照type排序的集合
	 */
	public Collection<HcBwpz> getOneGroupByType() {
		String hql = " from HcBwpz where deptId=:deptid and invalid = false group by type";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deptid", this.getDeptId());
		Collection<HcBwpz> hcBwpzs = this.query(hql, param);
		return hcBwpzs;
	}

	/*
	 * 以deptid为参数，得到实体集合，并根据pztemplate排序；
	 */
	public Collection<HcBwpz> getMuByDeptName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		String hql = "from HcBwpz where deptId=:deptid and invalid = false order by pzTemplate ";
		return this.query(hql, map);
	}

	// 之前用到了，现在用不到，舍不得删除！
	// private String getDeptName() {
	// IUser loginuser = ContextHolder.getLoginUser();
	// if (loginuser == null)
	// throw new NoneLoginException("您未登录，请刷新页面后重试！！");
	// if (loginuser.getDepts() == null || loginuser.getDepts().size() <= 0)
	// throw new NoneLoginException("您未设置部门，请联系管理员设置部门后再试！！");
	// List<IDept> depts = loginuser.getDepts();
	// String deptname = null;
	// if (depts == null || depts.isEmpty()) {
	// deptname = depts.get(0).getName();
	// }
	//
	// return deptname;
	//
	// }

	// 方法扔到baseimagedao裏面了
	// private String getDeptId() {
	// IUser loginuser = ContextHolder.getLoginUser();
	// List<IDept> depts = loginuser.getDepts();
	// String deptid = null;
	// if (depts == null || depts.isEmpty()) {
	// } else {
	// deptid = depts.get(0).getId();
	// }
	// return deptid;
	//
	// }

	// 使用父类的方法，传code，得到xy的坐标
	public List<HcpzXY> getHcPzXYs(Integer pzcode) {
		List<HcpzXY> lists = getHcPzXYsBase(pzcode);
		return lists;
	}

	// 使用父类的方法，传code，保存Hcpzyz，保存坐标
	public void saveBwpzXY(Collection<HcpzXY> hcbwpzxys) {
		saveXjpzXYBase(hcbwpzxys, PZ_CODE_);
	}

	// 使用父类的方法，传code，font，并且更新方法
	public void updateFont(PzFont pzfont) {
		updateFontBase(pzfont, PZ_CODE_);
	}

	// 删除模板
	public void deleteTemplateById(String id) {
		// String hql = "update HcXjpz where id=" + id;
		// HcXjpz entity = new HcXjpz();
		// entity.setId(id);
		this.delete(id, HcBwpz.class);
	}

	/**
	 * 更新模板名稱
	 * 
	 * @auther jack.zhao
	 * @param templatename
	 * @param id
	 * @return
	 */
	public boolean updateTemplateNameDao(final String templatename, final String id) {

		// 根據id得到type
		String sql1 = "from HcBwpz where id=:id and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		List<HcBwpz> HcBwpzs1 = query(sql1, param);
		String typestr = HcBwpzs1.get(0).getType();

		// 根據type查找所有的pztemplate是否用相同的，
		String sql = "from HcBwpz where  invalid = false and type=:type ";
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("type", typestr);

		Collection<HcBwpz> HcBwpzs = this.query(sql, param1);
		for (HcBwpz hcbwpz : HcBwpzs) {
			if (hcbwpz.getPzTemplate().equals(templatename)) {
				return false;
			}
		}
		// 更新操作
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			String hql = "update HcBwpz set pzTemplate=:pzTemplate  where id=:id ";

			@Override
			public Boolean doInSession(Session session) {
				Query query = session.createQuery(hql);
				query.setString("pzTemplate", templatename);
				query.setString("id", id);
				query.executeUpdate();
				return true;
			}
		});

	}

	/**
	 * 更新模板類型
	 * 
	 * @auther jack.zhao
	 * @param templatetype
	 * @param id
	 * @return
	 */
	public boolean updateTemplateTypeDao(final String templatetype, final String id) {
		// 判斷類型是否有相同的
		String sql = "select count(*) from HcBwpz where type=:type and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", templatetype);
		int count = this.queryForInt(sql, param);
		if (count > 0) {
			return false;
		}
		// 更新類型
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			@Override
			public Boolean doInSession(Session session) {

				String sql = "from HcBwpz where id=:id and invalid = false ";
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", id);
				List<HcBwpz> HcBwpzs = query(sql, param);
				String typestr = HcBwpzs.get(0).getType();

				String sql1 = "from HcBwpz where type=:type and invalid = false ";
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("type", typestr);
				List<HcBwpz> HcBwpzs1 = query(sql1, param1);
				for (HcBwpz hcbwpz : HcBwpzs1) {
					String hql2 = "update HcBwpz set type=:type  where id=:id ";
					Query query = session.createQuery(hql2);
					query.setString("type", templatetype);
					query.setString("id", hcbwpz.getId());
					query.executeUpdate();
				}

				return true;
			}

		});

	}

}
